const config = require('../config.js')

module.exports = (success, error)=> {
  
  if(typeof error !== 'function'){
    error = ()=> console.log("连接失败");
    return;
  }
  
  const mongoose = require('mongoose');
  
  mongoose.connect(`mongodb://${config.DBHOST}:${config.DBPOST}/${config.DBNAME}`);
  
  mongoose.connection.once('open', ()=> {
    success();
  })
  
  mongoose.connection.on('error', ()=> {
    error();
  })
  
  mongoose.connection.on('close', ()=> {
    console.log("数据库连接已断开");
  })
}